package com.example.woke1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.LoadActivity;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.data.Datas_event2;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import cn.xl.xxl.imagecache.ImageLoader;
import org.greenrobot.eventbus.EventBus;

public class FriendinfoActivity extends Activity {

	private String userid;
	private TextView mTname,mTphone,mTpai;
	private ImageView mIcon;
	private ImageLoader imageLoader;
	private TextView mTok;
	private MyApp application;
	private String avatar;//ͷ���ַ
	private String user_login;//�ֻ��
	private String user_nicename;//�û���
	private RelativeLayout mRelatvie,mRelatvie1;
	private TextView mTstats;
	private int closed;
	private Intent intent;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_friendinfo);
		
		
		EventBus.getDefault().register(this);
		intent = getIntent();
		userid = intent.getStringExtra("userid");
		closed = intent.getIntExtra("closed", 0);
		imageLoader = new ImageLoader(this);
		intview();
		
	}
	
	@Override
	protected void onDestroy() {
		imageLoader.clearCache();
		imageLoader.clearMemoryCache();
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}

	private void intview() {
		application = (MyApp) getApplication();
		findViewById(R.id.avfriendinfo_image_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
			finish();	
			}
		});
		
		mRelatvie = (RelativeLayout) findViewById(R.id.avfriendinfo_frame);
		mRelatvie1 = (RelativeLayout) findViewById(R.id.avfriendinfo_frame1);
		
		mTok = (TextView) findViewById(R.id.avfriendinfo_ok);
		mTok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				if(application.getDatas_load ()==null)return;
				if(closed == 1){
					if(application.getDatas_load().getUser_login().equals(user_login)){
						Toast.makeText(FriendinfoActivity.this, "不可以添加自己!", Toast.LENGTH_SHORT).show();
					}else{
						addfreidn();
					}
				}else{
					Intent intent = new Intent(FriendinfoActivity.this,PaytofriendActivity.class);
					intent.putExtra("userid", userid);
					intent.putExtra("avatar", avatar);
					intent.putExtra("user_login", user_login);
					intent.putExtra("user_nicename", user_nicename);
					startActivity(intent);
				}
			}
		});
		
		TextView mTdelete = (TextView) findViewById(R.id.avfriendinfo_image_deletef);
		mTdelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				//ɾ�����
				deletefreidn();
			}
		});
		
		mTname = (TextView) findViewById(R.id.avfriendinfo_textname);
		mTphone = (TextView) findViewById(R.id.avfriendinfo_phone);
		mTpai = (TextView) findViewById(R.id.avfriendinfo_pai);
		mIcon = (ImageView) findViewById(R.id.avfriendinfo_image_inco);
		mTstats = (TextView) findViewById(R.id.avfriendinfo_text_statces);
		if(closed == 1){
			mTdelete.setVisibility(View.GONE);
			mTok.setText("添加");
		}
		
		if(null == application.getDatas_load()){
			intent.setClass(FriendinfoActivity.this, LoadActivity.class);
			startActivityForResult(intent, 657);
		}else{
			getfriendinfo();
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onEventMainThread(Datas_event2 event) {
		String str1 = event.str1;
		if(str1.equals("0")){
			finish();
		}
	}

	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(requestCode == 657 && resultCode == 658){
				getfriendinfo();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	private void addfreidn() {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(FriendinfoActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "addNearby");
		params.put("userid", application.getDatas_load().getId());
		params.put("touserid", userid);
		
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("params", response.toString());
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("success")){
//						JSONObject object = response.getJSONObject("data");
//						JSONObject jsonObject = object.getJSONObject("data");
						Toast.makeText(FriendinfoActivity.this, info, Toast.LENGTH_SHORT).show();
						mTok.setText("等待对方验证");
						mTok.setClickable(false);
//						finish();
					}else{
						Toast.makeText(FriendinfoActivity.this, info, Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
//				Log.e("�ϴ�", "�ֻ��ʧ��"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mRelatvie1.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mRelatvie1.setVisibility(View.GONE);
			}
		});
	}
	
	private void deletefreidn() {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(FriendinfoActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "delFriden");
		params.put("userid", application.getDatas_load().getId());
		params.put("deluser", userid);
		Log.e("params", params.toString());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("删除好友", "删除"+response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("success")){
//						JSONObject object = response.getJSONObject("data");
//						JSONObject jsonObject = object.getJSONObject("data");
						Toast.makeText(FriendinfoActivity.this, info, Toast.LENGTH_SHORT).show();
						finish();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
//				Log.e("�ϴ�", "�ֻ��ʧ��"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mRelatvie1.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mRelatvie1.setVisibility(View.GONE);
			}
		});
	}

	private void getfriendinfo() {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(FriendinfoActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "fridenDetails");
		params.put("userid", userid);
		Log.e("params", params.toString());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){

			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("好友详情", "详情"+response);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						JSONObject object = response.getJSONObject("data");
						JSONObject jsonObject = object.getJSONObject("data");
//						JSONArray jsonArray = object.getJSONArray("data");
//						for (int i = 0; i < jsonArray.length(); i++) {
//							JSONObject object2 = jsonArray.getJSONObject(i);
							String id = jsonObject.getString("id");
							String user_no_status = jsonObject.getString("user_no_status");
							avatar = jsonObject.getString("avatar");
							user_login = jsonObject.getString("user_login");
							String user_type = jsonObject.getString("user_type");
							user_nicename = jsonObject.getString("user_nicename");
							
							if(application.getDatas_load().getUser_login().equals(user_login)){
								mTok.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightred));
							}
							
							
							if(user_no_status.equals("3")){
								
							}else if(user_no_status.equals("1")){
								mTstats.setText("未认证");
								mTstats.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightgray));
							}else{
								mTstats.setText("认证审核中");
								mTstats.setBackgroundDrawable(getResources().getDrawable(R.drawable.rect_lightgray));
							}
							mTname.setText(user_nicename);	
							imageLoader.DisplayImage("http://"+avatar, mIcon, false, true, true,0);
							mTphone.setText(user_login.substring(0, 3)+"****"+user_login.subSequence(user_login.length()-4, user_login.length()));
							if(user_type.equals("2")){
								mTpai.setText("铜牌会员");
							}else if(user_type.equals("3")){
								mTpai.setText("银牌会员");
							}else if(user_type.equals("4")){
								mTpai.setText("金牌会员");
							}else{
								mTpai.setText("代理商");
							}
					}else{
						Toast.makeText(FriendinfoActivity.this, "好友数据获取失败", Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(FriendinfoActivity.this, "好友数据获取失败", Toast.LENGTH_SHORT).show();
				finish();
//				Log.e("�ϴ�", "�ֻ��ʧ��"+responseString);
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mRelatvie.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mRelatvie.setVisibility(View.GONE);
			}
		});
	}
}
